/*
Copyright (c) 2010 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.Drawing;
using System.Web.UI;
using System.ComponentModel.Design.Serialization;

namespace SCS.Web.UI.WebControls
{
	public class ContextHotSpot : IStateManager
	{
		private bool isTrackingViewState;
		private StateBag viewState;

		private string name = "ContextHotSpot";
		private string clientId = "";
		private int childDepth = 5;

		public ContextHotSpot()
		{		
		}

		public ContextHotSpot(string clientId)
		{
			this.clientId = clientId;
		}

		public ContextHotSpot(string clientId, string name)
		{
			this.clientId = clientId;
			this.name = name;
		}
		public ContextHotSpot(string clientId, string name, int childDepth)
		{
			this.clientId = clientId;
			this.name = name;
			this.childDepth = childDepth;
		}
		public ContextHotSpot(string clientId, int childDepth)
		{
			this.clientId = clientId;
			this.childDepth = childDepth;
		}
				
		
		[Category("Behavior")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]  
		public string Name
		{
			get 
			{ 
				//if (this.clientId.Length > 0)
					//return string.Format("{0}: {1}, depth {2}", this.name, this.ClientId, this.childDepth); 
				//else
					return this.name;
			}
		}

		
		[Category("Behavior")]
		[DefaultValue(typeof(string), "")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public string ClientId
		{
			get { return this.clientId; }
			set { this.clientId = value; }
		}

		
		[Category("Behavior")]
		[DefaultValue(typeof(int), "5")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public int ChildDepth
		{
			get { return this.childDepth; }
			set { this.childDepth = value; }
		}

		
//		public string SaveToString()
//		{
//			return string.Format("{0} {1}", this.clientId, this.childDepth);
//		}
//
//		public void LoadFromString(string s)
//		{
//			string[] values = s.Split(' ');
//
//			this.clientId = values[0];
//			this.childDepth = int.Parse(values[1]);
//		}
//
	
		[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		protected StateBag ViewState 
		{
			get 
			{
				if (viewState == null) 
				{ 
					viewState = new StateBag(false);
					if (isTrackingViewState) ((IStateManager)viewState).TrackViewState();
				}
				return viewState;
			}
		}

		internal void SetDirty() 
		{
			if (viewState != null) 
			{
				ICollection Keys = viewState.Keys;
				foreach (string key in Keys) 
				{
					viewState.SetItemDirty(key, true);
				}
			}
		}

		
		public override string ToString() 
		{
			return GetType().Name; 
		}


		#region IStatemanager implementation
		
		bool IStateManager.IsTrackingViewState 
		{
			get 
			{
				return isTrackingViewState;
			}
		}

		void IStateManager.LoadViewState(object savedState) 
		{
			if (savedState != null) 
			{
				((IStateManager)ViewState).LoadViewState(savedState);

				string clientId = (string)ViewState["ClientId"];
				
				if (clientId != null)
					this.clientId = clientId;

				object childDepth = (string)ViewState["ChildDepth"];
				
				if (childDepth != null)
					this.childDepth = (int)childDepth;
			}
		}

		object IStateManager.SaveViewState() 
		{  
			string currentClientId = this.clientId;
			string initialClientId = (string)ViewState["ClientId"];
			
			if (currentClientId.Equals(initialClientId) == false) 
			{
				ViewState["ClientId"] = currentClientId;
			}
		
			int currentChildDepth = this.childDepth;
			int initialChildDepth = (int)ViewState["ChildDepth"];
			
			if (currentChildDepth.Equals(initialChildDepth) == false) 
			{
				ViewState["ChildDepth"] = currentChildDepth;
			}

			if (viewState != null) 
			{
				return ((IStateManager)viewState).SaveViewState();
			}
			return null;
		}

		void IStateManager.TrackViewState() 
		{
			if (this.clientId.Length > 0)
				ViewState["ClientId"] = this.clientId;
			
			if (this.childDepth > -1 && this.childDepth != 5)
				ViewState["ChildDepth"] = this.childDepth;
			
			isTrackingViewState = true;

			if (viewState != null) 
			{
				((IStateManager)viewState).TrackViewState();
			}
		}

		
		#endregion
	}
}

  


